package nancal.mp.service.jenkins.publish

import nancal.mp.db.mongo.PipelineTemplateEnum
import nancal.mp.service.jenkins.JenkinsPublishJob
import nbcp.base.utils.UrlUtil


fun JenkinsPublishJob.f5_check_k8s_status(): FunctionContent {
    if (this.appDeploySetting.pipelineTemplate == PipelineTemplateEnum.Native) {
        return FunctionContent.empty("check-k8s-status");
    }

    return FunctionContent(
        "check-k8s-status", """
echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "检查K8s服务状态:"
echo "curl  -o curl-result.txt  -s -w %{http_code} --max-time 6 --retry 3 ${appDeploySetting.mpIntegrationApiUrl}/ops/k8s/deployment/status/${this.task.id}?admin-token=${UrlUtil.encodeURIComponent(appDeploySetting.adminToken)}"

for i in {1..100} ; do
    code=＄(curl  -o curl-result.txt  -s -w %{http_code} --max-time 6 --retry 3 ${appDeploySetting.mpIntegrationApiUrl}/ops/k8s/deployment/status/${this.task.id}?admin-token=${UrlUtil.encodeURIComponent(appDeploySetting.adminToken)} ) 

    
    if [ "＄code" == "200" ] &&  grep -q '"code":0' curl-result.txt ; then
        echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "启动服务成功!"
        show-file curl-result.txt 50 ; echo 
        break;
    else
        if [ ＄i -ge 50 ]; then
            echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '启动服务失败!!! 返回: ＄code'
            show-file curl-result.txt 100 ; echo 
           
            exit 1
        fi
        sleep 5
    fi 
done
"""
    )
}
